﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'*******************************************************************

Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.Common

Public Class clsMyStrings

    '
    ' used to test port and slot names
    '
    Public Function isWellFormedName(ByVal strName As String) As Boolean

        Dim blnReturn As Boolean = False

        Try

            If strName = "" Then GoTo ExitHandler
            Dim strType As String

            Dim intDelCt As Integer
            Dim colString As Collection
            colString = New Collection

            intDelCt = ParseItemsToCollection(strName, colString, "_")

            If intDelCt <> 3 Then GoTo ExitHandler

            strType = LCase(colString.Item(1))
            ' rule 1 has to start with port
            If strType = "port" _
               Or strType = "slot" Then
            Else
                GoTo ExitHandler
            End If
            ' the final field must be numeric and greater than zero
            Dim strTemp As String
            strTemp = LCase(colString.Item(3))
            If Not (IsNumeric(strTemp)) Then
                GoTo ExitHandler
            End If
            If CInt(strTemp) < 1 Then
                GoTo ExitHandler
            End If

            ' if you made it this far it's ok
            blnReturn = True
ExitHandler:

        Catch ex As Exception

            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If


        Finally
            isWellFormedName = blnReturn
        End Try

    End Function




    '**************************************************************************
    '
    ' local string handlers
    '
    '**************************************************************************

    '
    '
    '
    Public Function memberOfCollection _
                (ByVal colObjects As Collection, _
                ByVal objMember As Object, _
                Optional ByVal strType As String = "string") _
                As Boolean

        Dim intX As Integer
        Dim blnReturn As Boolean = False
        Dim blnObject As Boolean = True

        Select Case strType
            Case "string"
                If Not (TypeOf objMember Is String) Then blnObject = False
                ' need to be explicit on com+ objects
            Case "int32"
                If Not (TypeOf objMember Is Int32) Then blnObject = False
            Case "double"
                If Not (TypeOf objMember Is Double) Then blnObject = False
            Case "integer"
                If Not (TypeOf objMember Is Integer) Then blnObject = False
                ' where long is 32bit on 32bit machine it becomes 64bit on upscale
            Case "long"
                If Not (TypeOf objMember Is Long) Then blnObject = False
            Case Else
                blnObject = False
        End Select

        Try
            ' we passed the objecttype test
            If blnObject = True Then
                For intX = 1 To colObjects.Count
                    Select Case strType
                        Case "string"
                            If LCase(objMember) = LCase(colObjects.Item(intX)) Then
                                blnReturn = True
                                Exit For
                            End If
                        Case "long", "double", "integer", "int32"
                            If objMember = colObjects.Item(intX) Then
                                blnReturn = True
                                Exit For
                            End If
                    End Select
                Next intX
            End If

        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally

            memberOfCollection = blnReturn

        End Try




    End Function

    '
    '
    '
    Public Sub removeStringFromCollection _
            (ByRef colStrings As Collection, _
            ByVal strRemove As String)

        Try

            Dim intX As Integer
            For intX = colStrings.Count To 1 Step -1
                If LCase(colStrings.Item(intX)) = LCase(strRemove) Then
                    colStrings.Remove(intX)
                End If
            Next intX
            Exit Sub
        Catch my_exception As Exception
            Debug.Print("removestringfromcollection " & my_exception.Message)
        End Try
    End Sub


    '
    '
    '
    Public Sub addStringToCollection _
            (ByRef colStrings As Collection, _
            ByVal strNew As String, _
            Optional ByVal blnUnique As Boolean = True)

        Dim intX As Integer
        Dim blnFound As Boolean
        blnFound = False

        '
        ' blnUnique ensures that the result is a list of unique names
        '
        If blnUnique = True Then
            For intX = 1 To colStrings.Count
                If colStrings.Item(intX) = strNew Then
                    blnFound = True
                    GoTo Found
                End If
            Next intX
        End If

Found:
        If blnFound = False Then
            colStrings.Add(strNew)
        End If

    End Sub

    '
    ' pass in a string, collection, and delimiter - update the collection and return count
    '
    Public Function ParseItemsToCollection _
        (ByVal s As String, _
        ByRef colA As Collection, _
        ByVal Delim As String, _
        Optional ByVal strType As String = "string") _
        As Integer

        Try

            '
            Dim p As Int32
            '
            ' Check for valid delimiter
            '
            If Delim = "" Then
                ParseItemsToCollection = -1
                Exit Function
            End If
            Dim intT32 As Int32

            '
            ' Copy Items
            '
            p = InStr(1, s, Delim, vbTextCompare)
            Do While p > 0

                Select Case strType
                    Case "string"
                        colA.Add(Left$(s, p - 1))
                    Case "integer"
                        colA.Add(CInt(Left$(s, p - 1)))
                    Case "double"
                        colA.Add(CDbl(Left$(s, p - 1)))
                    Case "int32"
                        intT32 = CInt(Left$(s, p - 1))
                        colA.Add(intT32)
                    Case "long"
                        colA.Add(CLng(Left$(s, p - 1)))
                End Select
                s = Mid$(s, p + 1)
                p = InStr(1, s, Delim, vbTextCompare)
            Loop
            '
            ' Copy Last Item
            '
            Select Case strType
                Case "string"
                    colA.Add(s)
                Case "integer"
                    colA.Add(CInt(s))
                Case "double"
                    colA.Add(CDbl(s))
                Case "int32"
                    intT32 = CInt(s)
                    colA.Add(intT32)
                Case "long"
                    colA.Add(CLng(s))
            End Select


        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            ParseItemsToCollection = colA.Count
        End Try

    End Function

    '
    ' create a csv string with end brackets from a collection
    '
    Public Function BuildCsvString _
        (ByVal colA As Collection) _
        As String

        Dim intY As Integer
        Dim strCsv As String = "[]"
        Try

            If (colA Is Nothing) _
                Or (colA.Count = 0) Then
                Exit Try
            Else
                With colA
                    ' start bracket
                    strCsv = "["
                    For intY = 1 To colA.Count
                        strCsv = strCsv & CStr(colA.Item(intY))
                        If intY <> colA.Count Then
                            strCsv = strCsv & ","
                        End If ' test for last position
                    Next intY
                    ' end bracket
                    strCsv = strCsv & "]"
                End With
            End If

        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
            strCsv = "[]"
        End Try

        BuildCsvString = strCsv

    End Function


    '
    ' pass in a bracketed string of csv separated items and pass back a collection
    ' strType allows the user to define what the collection is composed of
    '
    Public Function BuildCsvCollection _
            (ByVal strCsv As String, _
            Optional ByVal strType As String = "string", _
            Optional ByVal strDel As String = ",") _
            As Collection

        Dim colReturn As Collection
        colReturn = New Collection
        Try
            Dim intX As Integer

            ' get rid of first bracket
            If InStr(1, strCsv, "[") Then
                strCsv = Microsoft.VisualBasic.Replace(strCsv, "[", "")
            Else
                GoTo ExitHandler
            End If
            ' get rid of second bracket
            If InStr(1, strCsv, "]") Then
                strCsv = Microsoft.VisualBasic.Replace(strCsv, "]", "")
            Else
                GoTo ExitHandler
            End If

            intX = ParseItemsToCollection(strCsv, colReturn, strDel, strType)

ExitHandler:
            BuildCsvCollection = colReturn
            Exit Function

        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
            BuildCsvCollection = colReturn
        End Try

    End Function


    Function GetToken _
            (ByVal strS As String, _
            ByVal strD As String, _
            ByVal Indx As Integer) _
            As String

        Dim WC As Integer
        Dim Count As Integer
        Dim SPos As Integer
        Dim EPos As Integer
        Dim strReturn As String
        strReturn = ""

        On Error GoTo ErrHandler

        strS = LTrim(RTrim(strS))

        WC = CountTokens(strS, strD)

        If Indx < 1 Or WC < Indx Then
            GoTo ExitHandler
        End If

        Count = 1
        SPos = 1
        For Count = 2 To Indx
            SPos = InStr(SPos, strS, strD) + 1
        Next Count
        EPos = InStr(SPos, strS, strD) - 1
        If EPos <= 0 Then EPos = Len(strS)
        strReturn = Mid(strS, SPos, EPos - SPos + 1)

ExitHandler:

        GetToken = strReturn
        Exit Function

ErrHandler:

        GetToken = strReturn
        Debug.Print("GetToken Err " & Err.Description)

    End Function

    '
    '
    '
    Function CountTokens _
            (ByVal s As String, _
            ByVal D As String) _
            As Integer
        '
        ' Counts delimters in a string separated by input string D
        '
        Dim WC As Integer
        Dim Pos As Integer

        ' has to be a string, has to have a length
        If VarType(s) <> 8 _
              Or Len(s) < 1 _
              Or InStr(s, D) < 1 Then
            CountTokens = 0
            Exit Function
        End If

        WC = 0
        Pos = InStr(s, D)
        Do While Pos > 0
            WC = WC + 1
            Pos = InStr(Pos + 1, s, D)
        Loop

        ' tokens is equal to delimiters + 1
        CountTokens = WC + 1

    End Function




End Class
